/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.user.provider.hibernate3.ahxu.impl;

import com.opensymphony.user.provider.hibernate3.ahxu.dao.HibernateGroupDAO;
import com.opensymphony.user.provider.hibernate3.ahxu.dao.HibernateQueries;
import com.opensymphony.user.provider.hibernate3.ahxu.dao.SessionManager;
import com.opensymphony.user.provider.hibernate3.ahxu.entity.HibernateGroup;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Serializable;
import java.util.ArrayList;

import java.util.List;
import org.hibernate.impl.SessionImpl;


/**
 * @author <a href="mailto:matthew@linjafoo.com">Matthew E. Porter</a>
 */
public class HibernateGroupDAOImpl implements HibernateGroupDAO {
    //~ Instance fields ////////////////////////////////////////////////////////

    protected Log log = LogFactory.getLog(HibernateGroupDAOImpl.class.getName());
    private SessionManager sessionManager;

    //~ Constructors ///////////////////////////////////////////////////////////

    public HibernateGroupDAOImpl(SessionManager sessionManager) {
        this.sessionManager = sessionManager;
    }

    //~ Methods ////////////////////////////////////////////////////////////////

    public int deleteGroupByGroupname(String groupname) {
        int numberDeletedGroups = 0;
        SessionImpl session = null;//ahxu:Session->SessionImpl

        try {
            session = (SessionImpl)this.sessionManager.getSession();//ahxu:+(SessionImpl)
            numberDeletedGroups = session.delete(HibernateQueries.GROUP_BY_GROUPNAME, groupname, Hibernate.STRING);
        } catch (HibernateException he) {
            if (log.isDebugEnabled()) {
                log.debug("Unable to delete group with groupname " + groupname, he);
            }
        } finally {
            this.sessionManager.flushCloseSession(session);
        }

        return numberDeletedGroups;
    }

    public HibernateGroup findGroupByGroupname(String groupname) {
        HibernateGroup group = null;
        SessionImpl session = null;//ahxu

        try {
            session = (SessionImpl)this.sessionManager.getSession();

            List ret = session.find(HibernateQueries.GROUP_BY_GROUPNAME, groupname, Hibernate.STRING);

            if (ret.size() > 0) {
                group = (HibernateGroup) ret.get(0);
            }
        } catch (HibernateException he) {
            if (log.isDebugEnabled()) {
                log.debug("Unable to find group with groupname " + groupname, he);
            }

            group = null;
        } finally {
            this.sessionManager.closeSession(session);
        }

        return group;
    }

    public List findGroups() {
        List groups = new ArrayList();//ahxu
        SessionImpl session = null;//ahxu

        try {
            session = (SessionImpl)this.sessionManager.getSession();
            List found = session.find(HibernateQueries.ALL_GROUPS);
            if(found!=null) groups=found;
        } catch (HibernateException he) {
            if (log.isDebugEnabled()) {
                log.debug("HibernateException retrieving all groups.", he);
            }
        } finally {
            this.sessionManager.closeSession(session);
        }

        return groups;
    }

    public boolean saveGroup(HibernateGroup group) {
        boolean result = false;
        Serializable id = null;
        Session session = null;

        try {
            session = this.sessionManager.getSession();
            id = session.save(group);
            result = (id != null);
        } catch (HibernateException he) {
            if (log.isDebugEnabled()) {
                log.debug("HibernateException caught saving");
            }

            return false;
        } finally {
            try {
                session.flush();
            } catch (HibernateException e) {
                log.error("did not flush group", e);
            }

            this.sessionManager.flushCloseSession(session);
        }

        return result;
    }
}
